package com.navinfo.platform.basicdatastatistics.dto.dongfeng;

import com.navinfo.platform.basicdatastatistics.dto.ICustomDto;
import com.navinfo.platform.common.utils.DataUtil;
import lombok.Data;
import org.apache.spark.sql.Row;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import scala.collection.mutable.WrappedArray;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Data
public class CustomDto implements ICustomDto {

    /**
     * '百公里油耗，单位:L/100KM，100*trip_fuel/trip_mileage'
     */
    private  Long fuel100km;
    /**
     *  平均车速(不含怠速)，单位：KM/H，
     *  (trip_mileage/1000)/((trip_duration-idle_duration)/3600)'
     */

    private  Long avgSpeedNoidle;
    /**
     * 经济区间占比，单位：%，
     * eco_driving_duration/trip_duration*100'
     */
    private  Long  ecoProportion ;
    /**
     * 当日行程里程之和，单位：m，
     * ,1、行程时间大于等于5分钟,行程距离大于等于1千米,百公里油耗大于0小于80,总油耗大于0''
     */
    private  Long  tripMileageTotal ;
    /**
     * 刹车次数 ，单位：m，
     * ,刹车次数 "hsb_number+csb_number''
     */

    private  Long  brakeTimes;

    private Long sharpUpSpeed1Mileage     ;          //东风急加速距离 单位:M',
    private Long sharpUpSpeed1Fuel        ;          //东风急加速油耗 单位:ML',
    private Long sharpUpSpeed1Duration    ;          //东风急加速时长 单位:S',
    private Long sharpUpSpeed1Number      ;          //东风急加速次数',
    private Long sharpDownSpeed1Mileage   ;          //东风急减速距离 单位:M',
    private Long sharpDownSpeed1Fuel      ;          //东风急减速油耗 单位:ML',
    private Long sharpDownSpeed1Duration  ;          //东风急减速时长 单位: S',
    private Long sharpDownSpeed1Number    ;          //东风急减速次数',
    private Long sharpTurning1Number       ;          //东风急转弯次数',
    private Long longIdleFuel              ;          //东风超长怠速油耗 单位:ML',
    private Long longIdleDuration          ;          //东风超长怠速时长 单位:S',
    private Long longIdleNumber            ;          //东风超长怠速次数',
    private Long coldRunningMileage        ;          //东风冷车运行距离 单位:M',
    private Long coldRunningFuel           ;          //东风冷车运行油耗 单位:ML',
    private Long coldRunningDuration       ;          //东风冷车运行时长，单位:S',
    private Long coldRunningNumber         ;          //东风冷车运行次数',
    private Long nightRunningMileage       ;          //东风夜晚开车距离 单位:M',
    private Long nightRunningFuel          ;          //东风夜晚开车油耗 单位:ML',
    private Long nightRunningDuration      ;          //东风夜晚开车时长 单位:S',
    private Long nightRunningNumber        ;          //东风夜晚开车次数',
    private Long highRotationMileage       ;          //东风引擎高转速距离 单位:M',
    private Long highRotationFuel          ;          //东风引擎高转速油耗 单位:ML',
    private Long highRotationDuration      ;          //东风引擎高转速时长 单位:S',
    private Long highRotationNumber        ;          //东风引擎高转速次数',
    private Long largeGasMileage           ;          //东风大油门距离 单位:M',
    private Long largeGasFuel              ;          //东风大油门油耗 单位:ML',
    private Long largeGasDuration          ;          //东风大油门时长 单位:S',
    private Long largeGasNumber            ;          //东风大油门次数',
    private Long fullGasMileage            ;          //东风全油门距离 单位:M',
    private Long fullGasFuel               ;          //东风全油门油耗 单位:ML',
    private Long fullGasDuration           ;          //东风全油门时长 单位:S',
    private Long fullGasNumber             ;          //东风全油门次数',
    private Long fastSpeedMileage          ;          //东风高速行驶距离 单位:M',
    private Long fastSpeedFuel             ;          //东风高速行驶油耗 单位:ML',
    private Long fastSpeedDuration         ;          //东风高速行驶时长 单位:S',
    private Long fastSpeedNumber           ;          //东风高速行驶次数'
    private Long  neutralGearCoast1Mileage  ;        //东风空挡滑行距离 单位:M'
    private Long  neutralGearCoast1Fuel     ;        //东风空挡滑行油耗 单位:ML',
    private Long  neutralGearCoast1Duration ;        //东风空挡滑行时长 单位:S',
    private Long  neutralGearCoast1Number   ;        //东风空挡滑行次数',
    private Long  flameoutCoastMileage       ;        //东风熄火滑行距离 单位:M',
    private Long  flameoutCoastFuel          ;        //东风熄火滑行油耗 单位:ML',
    private Long  flameoutCoastDuration      ;        //东风熄火滑行时长 单位:S',
    private Long  flameoutCoastNumber        ;        //东风熄火滑行次数',

    /**
     * 按行程存储的载重信息列表
     */
    private List<LoadInfo> loadInfos;

    /**
     * 按行程存储的得分信息列表
     */
    private List<ScoreInfo> scoreInfos;

    @Override
    public void setCustomDtoInfo(Row row) {
        //计算百公里油耗,单位:L/100KM，100*trip_fuel/trip_mileage'
        calcFuel100km(row, this);
        //平均车速(不含怠速)，平均车速(不含怠速)，单位：KM/H，(trip_mileage/1000)/((trip_duration-idle_duration)/3600)
        calcAvgSpeedNoidle(row, this);
        //'经济区间占比，单位：%，eco_driving_duration/trip_duration*100'
        calcEcoProportion(row, this);
        //刹车次数 "hsb_number+csb_number
        calcBrakeTimes(row, this);

        this.setSharpUpSpeed1Mileage(row.getAs("sharp_up_speed1_mileage"));
        this.setSharpUpSpeed1Fuel(row.getAs("sharp_up_speed1_fuel"));
        this.setSharpUpSpeed1Duration(row.getAs("sharp_up_speed1_duration"));
        this.setSharpUpSpeed1Number(row.getAs("sharp_up_speed1_number"));
        this.setSharpDownSpeed1Mileage(row.getAs("sharp_down_speed1_mileage"));
        this.setSharpDownSpeed1Fuel(row.getAs("sharp_down_speed1_fuel"));
        this.setSharpDownSpeed1Duration(row.getAs("sharp_down_speed1_duration"));
        this.setSharpDownSpeed1Number(row.getAs("sharp_down_speed1_number"));
        this.setSharpTurning1Number(row.getAs("sharp_turning1_number"));
        this.setLongIdleFuel(row.getAs("long_idle_fuel"));
        this.setLongIdleDuration(row.getAs("long_idle_duration"));
        this.setLongIdleNumber(row.getAs("long_idle_number"));
        this.setColdRunningMileage(row.getAs("cold_running_mileage"));
        this.setColdRunningFuel(row.getAs("cold_running_fuel"));
        this.setColdRunningDuration(row.getAs("cold_running_duration"));
        this.setColdRunningNumber(row.getAs("cold_running_number"));
        this.setNightRunningMileage(row.getAs("night_running_mileage"));
        this.setNightRunningFuel(row.getAs("night_running_fuel"));
        this.setNightRunningDuration(row.getAs("night_running_duration"));
        this.setNightRunningNumber(row.getAs("night_running_number"));
        this.setHighRotationMileage(row.getAs("high_rotation_mileage"));
        this.setHighRotationFuel(row.getAs("high_rotation_fuel"));
        this.setHighRotationDuration(row.getAs("high_rotation_duration"));
        this.setHighRotationNumber(row.getAs("high_rotation_number"));
        this.setLargeGasMileage(row.getAs("large_gas_mileage"));
        this.setLargeGasFuel(row.getAs("large_gas_fuel"));
        this.setLargeGasDuration(row.getAs("large_gas_duration"));
        this.setLargeGasNumber(row.getAs("large_gas_number"));
        this.setFullGasMileage(row.getAs("full_gas_mileage"));
        this.setFullGasFuel(row.getAs("full_gas_fuel"));
        this.setFullGasDuration(row.getAs("full_gas_duration"));
        this.setFullGasNumber(row.getAs("full_gas_number"));
        this.setFastSpeedMileage(row.getAs("fast_speed_mileage"));
        this.setFastSpeedFuel(row.getAs("fast_speed_fuel"));
        this.setFastSpeedDuration(row.getAs("fast_speed_duration"));
        this.setFastSpeedNumber(row.getAs("fast_speed_number"));
        this.setNeutralGearCoast1Mileage(row.getAs("neutral_gear_coast1_mileage"));
        this.setNeutralGearCoast1Fuel(row.getAs("neutral_gear_coast1_fuel"));
        this.setNeutralGearCoast1Duration(row.getAs("neutral_gear_coast1_duration"));
        this.setNeutralGearCoast1Number(row.getAs("neutral_gear_coast1_number"));
        this.setFlameoutCoastMileage(row.getAs("flameout_coast_mileage"));
        this.setFlameoutCoastFuel(row.getAs("flameout_coast_fuel"));
        this.setFlameoutCoastDuration(row.getAs("flameout_coast_duration"));
        this.setFlameoutCoastNumber(row.getAs("flameout_coast_number"));
    }

    /***
     * 百公里油耗，单位:L/100KM，100*trip_fuel/trip_mileage
     * @param row
     * @param custom
     */
    private void calcFuel100km(Row row, CustomDto custom) {
        Long trip_fuel = row.getAs("trip_fuel");
        Long trip_mileage = row.getAs("trip_mileage");
        if(trip_mileage != 0){
            this.fuel100km = DataUtil.remainTwoBit(trip_fuel, trip_mileage);
        }else{
            this.fuel100km = new Long(0);
        }
    }

    /***
     * 平均车速(不含怠速)，单位：100* KM/H，
     *  (trip_mileage/1000)/((trip_duration-idle_duration)/3600)
     * @param row
     * @param custom
     */
    private static void calcAvgSpeedNoidle(Row row, CustomDto custom) {
        Long trip_mileage = row.getAs("trip_mileage");
        Long trip_duration = row.getAs("trip_duration");
        Long idle_duration =row.getAs("idle_duration");
        if(trip_duration-idle_duration!=0){
            custom.setAvgSpeedNoidle(DataUtil.remainTwoBit(trip_mileage * 3.6, (trip_duration - idle_duration)));
        }else{
            custom.setAvgSpeedNoidle(new Long(0));
        }
    }
    /***
     * 经济区间占比，单位：%，
     * eco_driving_duration/trip_duration*100'
     * @param row
     * @param custom
     */
    private static void calcEcoProportion(Row row, CustomDto custom) {
        Long eco_driving_duration = row.getAs("eco_driving_duration");
        Long trip_duration = row.getAs("trip_duration");
        if(trip_duration != 0 ){
            custom.setEcoProportion(DataUtil.remainTwoBit(eco_driving_duration, trip_duration));
        }else{
            custom.setEcoProportion(new Long(0));
        }


    }

    /***
     * 刹车次数 "hsb_number+csb_number
     * @param row
     * @param customDto
     */
    private static void calcBrakeTimes(Row row, CustomDto customDto){
        Long hsb_number = row.getAs("hsb_number");
        Long csb_number = row.getAs("csb_number");
        customDto.setBrakeTimes(Optional.ofNullable(hsb_number).orElse(new Long(0)) + Optional.ofNullable(csb_number).orElse(new Long(0)));

    }
}
